import os
from copyreg import add_extension

from fastapi import FastAPI
from tortoise.contrib.fastapi import register_tortoise

# 数据库配置
DB_ORM_CONFIG = {
    "connections": {
        "base": {
            "engine": "tortoise.backends.mysql",
            "credentials": {
                "host": os.getenv('BASE_HOST','127.0.0.1'),
                "user": os.getenv("BASE_USER","root"),
                "password": os.getenv("BASE_PASSWORD","root"),
                "port": int(os.getenv("BASE_PORT",3306)),
                "database": os.getenv("BASE_DB","base"),
            }
        },
        # "db2": {
        #     "engine": "tortoise.backends.mysql",
        #     "credentials": {
        #         "host": os.getenv('BASE_HOST','127.0.0.1'),
        #         "user": os.getenv("BASE_USER","root"),
        #         "password": os.getenv("BASE_PASSWORD","root"),
        #         "port": int(os.getenv("BASE_PORT",3306)),
        #         "database": os.getenv("BASE_DB","base"),
        #     }
        # },
        # "db3": {
        #     "engine": "tortoise.backends.mysql",
        #     "credentials": {
        #         "host": os.getenv('BASE_HOST','127.0.0.1'),
        #         "user": os.getenv("BASE_USER","root"),
        #         "password": os.getenv("BASE_PASSWORD","root"),
        #         "port": int(os.getenv("BASE_PORT",3306)),
        #         "database": os.getenv("BASE_DB","base"),
        #     }
        # },
    },
    "apps": {
        "base": { "models": ["models.base"],"default_connection": "base" },
        # "db2": {"models": ["models.base"], "default_connection": "base"},
        # "db3": {"models": ["models.base"], "default_connection": "base"},
    },
    "use_tz": False,
    "timezone": "Asia/Shanghai"
}

async def register_mysql(app: FastAPI):
    # 连接数据库
    register_tortoise(
        app,
        config=DB_ORM_CONFIG,
        generate_schemas=True,
        add_exception_handlers=True,
    )
